package com.example.text.service.serviceImpl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.text.dao.AdminRoleDao;
import com.example.text.entity.*;
import com.example.text.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdminRoleServiceImpl extends ServiceImpl<AdminRoleDao, AdminRole> implements AdminRoleService {


    @Autowired
    AdminUserRoleService adminUserRoleService;

    @Autowired
    AdminMenuService adminMenuService;

    @Autowired
    AdminRoleDao adminRoleDao;

    @Autowired
    AdminRoleService adminRoleService;

    @Autowired
    AdminPermissionService adminPermissionService;

    @Autowired
    AdminRolePermissionService adminRolePermissionService;

    @Autowired
    AdminRoleMenuService adminRoleMenuService;

    public List<AdminRole> listRolesByUser(String username) {

        // 根据uid，获得当前用户对应的所有角色规则的 rid 列表
        List<Integer> rids = adminUserRoleService.listAllByUid(username)
                .stream().map(AdminUserRole::getRid).collect(Collectors.toList());
        if(!rids.isEmpty()){
            return adminRoleDao.findAllListByRids(rids);
        }else {
            return null;
        }


    }

    public IPage<AdminRole> selectAdminRolePage(Page<AdminRole> page){

        IPage<AdminRole> adminRoleIPage= adminRoleDao.selectPageVo(page);
        System.out.println("管理员规则：:"+adminRoleIPage);

        List<AdminPermission> perms;
        List<AdminMenu> menus;
        for (AdminRole role : adminRoleIPage.getRecords()) {
            perms = adminPermissionService.listPermsByRoleId(role.getId());
            menus = adminMenuService.getMenusByRoleId(role.getId());
            System.out.println("menus："+menus);
            role.setPerms(perms);
            role.setMenus(menus);
        }


        return adminRoleIPage;
    }

    public List<AdminRole> selectAll(){

        List<AdminRole> adminRoles=adminRoleDao.findAll();

        List<AdminPermission> perms;
        List<AdminMenu> menus;
        for (AdminRole role : adminRoles) {
            perms = adminPermissionService.listPermsByRoleId(role.getId());
            menus = adminMenuService.getMenusByRoleId(role.getId());
            role.setPerms(perms);
            role.setMenus(menus);
        }
        return adminRoles;
    }

    public AdminRole updateRoleStatus(AdminRole role) {
        AdminRole roleInDB = adminRoleDao.selectById(role.getId());
        roleInDB.setEnabled(role.isEnabled());
         adminRoleDao.updateById(roleInDB);
         return roleInDB;
    }

    public void addOrUpdate(AdminRole adminRole) {

        adminRoleService.saveOrUpdate(adminRole);
    }

    public void editRole(@RequestBody AdminRole role) {
        adminRoleDao.insert(role);
        adminRolePermissionService.savePermChanges(role.getId(), role.getPerms());
    }

   public  void deleteRole(AdminRole requestRole){

       int id=requestRole.getId();
       adminRoleDao.deleteById(id);

       //删除对应的菜单
       adminRoleMenuService.remove(new QueryWrapper<AdminRoleMenu>().eq("rid",id));

       //删除对应的功能配置
       adminRolePermissionService.remove(new QueryWrapper<AdminRolePermission>().eq("rid",id));


   }

   public  void deleteRoles(String[] RoleIds){

            adminRoleDao.deleteBatchIds(Arrays.asList(RoleIds));

            for( String roleId : RoleIds){
                adminRoleMenuService.remove(new QueryWrapper<AdminRoleMenu>().eq("rid",roleId));
                //删除对应的功能配置
                adminRolePermissionService.remove(new QueryWrapper<AdminRolePermission>().eq("rid",roleId));

            }
   }
}
